package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Admin {
    private JFrame frame;
    public Admin() {
        frame = new JFrame("游戏");
        frame.setSize(250, 320);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        //创建并设置图片
        ImageIcon originalIcon = new ImageIcon("D:\\图片\\Saved Pictures\\ball.jpg"); //替换为图片的路径
        Image scaledImage = originalIcon.getImage().getScaledInstance(250, 250, Image.SCALE_SMOOTH);
        ImageIcon scaledIcon = new ImageIcon(scaledImage);
        JLabel imageLabel = new JLabel(scaledIcon);
        frame.add(imageLabel, BorderLayout.CENTER);
        //创建面板来放置按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        //创建开始游戏按钮
        JButton startButton = new JButton("开始游戏");
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //处理开始游戏按钮的点击事件
                JOptionPane.showMessageDialog(frame, "游戏开始");
                BallGame.runGame();
            }
        });
        buttonPanel.add(startButton);
        //创建排行榜按钮
        JButton leaderboardButton = new JButton("排行榜");
        leaderboardButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //处理排行榜按钮的点击事件
                showLeaderboard();
            }
        });
        buttonPanel.add(leaderboardButton);
        frame.add(buttonPanel, BorderLayout.SOUTH);
        //创建退出按钮
        JButton exitButton = new JButton("退出");
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //处理退出按钮的点击事件
                frame.dispose(); //关闭Admin窗口
            }
        });
        buttonPanel.add(exitButton); //将退出按钮添加到按钮面板
        frame.add(buttonPanel, BorderLayout.SOUTH);
        frame.setVisible(true);
    }
    private void showLeaderboard() {
        JFrame leaderboardFrame = new JFrame("排行榜");
        leaderboardFrame.setSize(400, 300);
        leaderboardFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        leaderboardFrame.setLayout(new GridLayout(11, 3));
        JLabel tmpRankLabel=new JLabel("排名");
        JLabel tmpNameLabel=new JLabel("用户");
        JLabel tmpScoreLabel=new JLabel("分数");
        leaderboardFrame.add(tmpRankLabel);
        leaderboardFrame.add(tmpNameLabel);
        leaderboardFrame.add(tmpScoreLabel);
        //创建排行榜前十数据
        BoardDao boardDao=new BoardDaoImpl();
        ArrayList <Board> boards=boardDao.run();
        int sum=10;
        if(boards.size()<sum) sum=boards.size();
        for(int i=0;i<sum;i++){
            JLabel rankLabel=new JLabel(Integer.toString(i+1));
            JLabel nameLabel=new JLabel(boards.get(i).getName());
            JLabel scoreLabel=new JLabel(Integer.toString(boards.get(i).getScore()));
            leaderboardFrame.add(rankLabel);
            leaderboardFrame.add(nameLabel);
            leaderboardFrame.add(scoreLabel);
        }
        for(int i=sum;i<10;i++){
            JLabel rankLabel=new JLabel(Integer.toString(i+1));
            JLabel nameLabel=new JLabel("null");
            JLabel scoreLabel=new JLabel("null");
            leaderboardFrame.add(rankLabel);
            leaderboardFrame.add(nameLabel);
            leaderboardFrame.add(scoreLabel);
        }
        leaderboardFrame.setVisible(true);
    }
    public static void runAdmin(){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Admin();
            }
        });
    }
}